/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.App;
import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.db.dao.UserDao;
import com.gmail.blackdog1987.carcare.entity.TProduct;
import com.gmail.blackdog1987.carcare.entity.TPromotion;
import com.gmail.blackdog1987.carcare.network.request.builder.PromotionJoinRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.OrderCarRentResponse;
import com.gmail.blackdog1987.carcare.network.response.PromotionJoinResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.gmail.blackdog1987.carcare.network.response.handler.PromotionJoinResponseHandler;

public class PromotionJoinActivity extends BaseActivity implements OnResponseListener, OnClickListener {
	private Context mContext;

	CubeImageView head;
	TextView title;
	TextView time;
	TextView price;
	TextView content;
	EditText name;
	EditText phone;
	EditText idcard;
	EditText idtype;
	EditText driveage;
	View pay;
	TextView cost;
	TPromotion promotion;

	PromotionJoinRequestBuilder req = new PromotionJoinRequestBuilder();
	PromotionJoinResponseHandler handler = new PromotionJoinResponseHandler();

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_promotionjoin);
		mContext = this;

		initViews();
		initData(savedInstanceState);
		reg();
	}

	public void initViews() {
		head = (CubeImageView) findViewById(R.id.promotion_join_head);
		title = (TextView) findViewById(R.id.promotion_join_title);
		time = (TextView) findViewById(R.id.promotion_join_time);
		price = (TextView) findViewById(R.id.promotion_join_price);
		content = (TextView) findViewById(R.id.promotion_join_content);
		name = (EditText) findViewById(R.id.promotion_join_name);
		phone = (EditText) findViewById(R.id.promotion_join_phone);
		idcard = (EditText) findViewById(R.id.promotion_join_idcard);
		idtype = (EditText) findViewById(R.id.promotion_join_idtype);
		driveage = (EditText) findViewById(R.id.promotion_join_driveage);
		pay = findViewById(R.id.promotion_join_pay);
		cost = (TextView) findViewById(R.id.promotion_join_cost);
	}

	public void reg() {
		handler.setOnResponseListener(this);
		findViewById(R.id.back).setOnClickListener(this);
		findViewById(R.id.promotion_join_pay).setOnClickListener(this);
	}

	public void initData(Bundle savedInstanceState) {
		promotion = (TPromotion) getIntent().getSerializableExtra("data");
		if (promotion.getImgurl() != null && promotion.getImgurl().length() > 0) {
			ImageLoader imageLoader = ImageLoaderFactory.create(mContext);
			App.loadImg(head, imageLoader, promotion.getImgurl());
		}
		title.setText(promotion.getTitle());

		SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:");
		time.setText(dateformat1.format(promotion.getStarttime()) + " 到 " + dateformat1.format(promotion.getEndtime()));
		price.setText(String.valueOf(promotion.getPrice()));
		content.setText(promotion.getContent());
		cost.setText(String.valueOf(promotion.getPrice()));
		if (phone!=null&&App.validUser(this)){
			phone.setText(new UserDao().getUser().username);
		}
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if (phone!=null&&App.validUser(this)){
			phone.setText(new UserDao().getUser().username);
		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.promotion_join_pay:
			if (!App.validUser(this))
				return;
			req.setDriveage(driveage.getText().toString()).setIdcard(idcard.getText().toString()).setName(name.getText().toString())
					.setPhone(phone.getText().toString()).setPromotion_id(String.valueOf(promotion.getId())).postJsonRequest(mContext, handler);
			break;
		default:
			break;
		}
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case PayActivity.REQUESTCODE_PAY:
				Toast.makeText(mContext, "恭喜您支付成功,验证码将通过短信发给您", Toast.LENGTH_LONG).show();
				finish();
				break;
			}
		}
	}

	@Override
	public void onResponse(IResponseHandler classz) {
		if(promotion.getPrice()<=0){
			Toast.makeText(mContext, "报名成功，验证码将通过短信发给您", Toast.LENGTH_LONG).show();
			finish();
		}else{
			TProduct product = new TProduct();
			product.body = "汽车租赁";
			product.price = promotion.getPrice();
			product.subject = "车拉丁";
			product.tableName = "t_promotion_join";
			product.tableId = ((PromotionJoinResponse) classz.getResponse()).data.getId();
			product.userid = new UserDao().getUser().id;
			Intent i = new Intent(PromotionJoinActivity.this, PayActivity.class);
			i.putExtra("data", product);
			i.putExtra("url", promotion.getImgurl());
			startActivityForResult(i, PayActivity.REQUESTCODE_PAY);
		}
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(mContext, "下单失败", Toast.LENGTH_LONG).show();
	}
}
